<#
.SYNOPSIS
    One-step build script for Windows (PowerShell) to build the Go program in this repo.

.DESCRIPTION
    This script builds the Go program `osd_cpu_bind.go` into the `dist` directory. It supports
    local builds and cross-compilation by setting GOOS and GOARCH. It mirrors the intent of
    the existing `build_binary.sh` but for PowerShell on Windows.

.PARAMETER Output
    Output binary name (default: osd_cpu_bind)

.PARAMETER Targets
    A comma separated list of targets in the form os/arch (examples: windows/amd64, linux/amd64, linux/arm64).

.PARAMETER Strip
    If specified, attempt to strip the binary using llvm-strip/strip if available in PATH.

.EXAMPLE
    .\build.ps1
    Builds for current host and puts result in ./dist

    .\build.ps1 -Targets "windows/amd64,linux/amd64"
    Builds two cross targets (requires Go with GOOS/GOARCH support)
#>

param(
    [string]$Output = "osd_cpu_bind",
    [string]$Targets = "",
    [switch]$Strip
)

Set-StrictMode -Version Latest

$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Push-Location $scriptDir

if (-not (Test-Path -Path "dist")) {
    New-Item -ItemType Directory -Path "dist" | Out-Null
}

function Build-One {
    param(
        [string]$goos,
        [string]$goarch,
        [string]$outfile
    )

    Write-Host "Building for $goos/$goarch -> $outfile"

    $env:GOOS = $goos
    $env:GOARCH = $goarch

    # ensure deterministic output name
    $ext = if ($goos -eq 'windows') { '.exe' } else { '' }
    $targetPath = Join-Path -Path "dist" -ChildPath ($outfile + $ext)

    $ldflags = "-s -w"
    $buildCmd = "go build -ldflags='$ldflags' -o `"$targetPath`" osd_cpu_bind.go"

    Write-Host "Running: $buildCmd"
    $proc = Start-Process -FilePath "go" -ArgumentList @("build","-ldflags=$ldflags","-o",$targetPath,"osd_cpu_bind.go") -NoNewWindow -Wait -PassThru -ErrorAction Stop

    if ($Strip) {
        # try llvm-strip then strip
        if (Get-Command "llvm-strip" -ErrorAction SilentlyContinue) {
            & llvm-strip $targetPath
        } elseif (Get-Command "strip" -ErrorAction SilentlyContinue) {
            & strip $targetPath
        }
    }

    if (-not (Test-Path $targetPath)) {
        throw "Build failed, output $targetPath not found"
    }

    Write-Host "Built: $targetPath"
}

if ([string]::IsNullOrWhiteSpace($Targets)) {
    # build for host
    $goinfo = & go env GOOS GOARCH 2>$null
    $parts = $goinfo -split "\n"
    $hostOS = $parts[0].Trim()
    $hostArch = $parts[1].Trim()
    Build-One -goos $hostOS -goarch $hostArch -outfile $Output
} else {
    $targets = $Targets -split ","
    foreach ($t in $targets) {
        $t = $t.Trim()
        if ($t -match "^([^/]+)/([^/]+)$") {
            $goos = $matches[1]
            $goarch = $matches[2]
            $out = "$Output-$goos-$goarch"
            Build-One -goos $goos -goarch $goarch -outfile $out
        } else {
            Write-Warning "Invalid target format: $t. Expect os/arch"
        }
    }
}

Pop-Location
